package com.example.beat_x3.Fragments;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.beat_x3.Adapter.VenueAdapter;
import com.example.beat_x3.Client.GetResult;
import com.example.beat_x3.Client.LoadImages;
import com.example.beat_x3.R;

import Entity.Inf;
import Entity.Place;
import Entity.Places;

public class VenueFragment extends MyFragment {
    PlaceScheduleFragment placeScheduleFragment;
    private VenueAdapter venueAdapter;

    private final Handler myHandler = new Handler(Looper.getMainLooper()) {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) venueAdapter.notifyDataSetChanged();
        }
    };

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.fragment_venue, null);
        placeScheduleFragment = new PlaceScheduleFragment();
        Inf.placeScheduleFragment = placeScheduleFragment;
        SwipeRefreshLayout refreshLayout = view.findViewById(R.id.venue_refresh);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh(1000);
                myHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.setRefreshing(false);
                    }
                }, 1000);
            }
        });
        RecyclerView recyclerView = view.findViewById(R.id.venue_re1);
        venueAdapter = new VenueAdapter();
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(venueAdapter);
        venueAdapter.setOnItemClickListener(new VenueAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(String placeNumber, String placeImageSrc) {
                Inf.placeSchedule_in = true;
                Toast.makeText(Inf.context, "PLACE" + placeNumber, Toast.LENGTH_SHORT).show();
                placeScheduleFragment.refresh(placeNumber, placeImageSrc);
                changeFragment(placeScheduleFragment);
            }
        });
        refresh(0);
        return view;
    }

    public void refresh(int delay) {
        new RefreshThread(delay).start();
    }

    class RefreshThread extends Thread {
        int delay;

        public RefreshThread(int delay) {
            this.delay = delay;
        }

        @Override
        public void run() {
            Places places = GetResult.QueryPlaces();
            if (places != null) {

                venueAdapter.places = places.places;
                if (Inf.place_images != null) {
                    for (Place x : places.places) {
                        if (!Inf.place_images.containsKey(x.PlaceImage))
                            LoadImages.downloadImages(LoadImages.PLACE_SRC, x.PlaceImage);
                    }
                }
                try {
                    sleep(delay);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                myHandler.sendEmptyMessage(0);
            }
        }
    }
}
